package com.wang.exam.controller;

import com.wang.exam.pojo.Student;
import com.wang.exam.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.swing.plaf.synth.SynthFormattedTextFieldUI;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MessageController {
    public String keyword;                                   //全局变量，提升作用域
    public String studentKey;
    public Integer major_key;
    @Autowired
    com.wang.exam.service.loginService loginService;
    @RequestMapping("/teacherList")
    @ResponseBody                                           //向浏览器返回json数据
    public Map<String,Object> teacherList(int page , int rows){
        Map<String,Object> map  = new HashMap<>();
        int n = loginService.getCount();
        List<Teacher> teacherList = loginService.getTeacherList(page,rows);
        map.put("rows",teacherList);
        map.put("total",n);
        return map;
    }
    @RequestMapping("/deleteTeachers")
    @ResponseBody
    public Object deleteTeachers(@RequestBody String ids){
        String str = ids.substring(1,ids.length()-1);
        String[] strings = str.split("\\.");
        loginService.deleteId(strings);
        Map<String,String> map = new HashMap<String,String>();
        map.put("code","200");
        map.put("msg","批量删除成功！");
        return map;
    }
    @RequestMapping("/add_teacher_log")
    @ResponseBody
    public Object addTeacher(@RequestParam("teacher_name")String teacher_name,
                             @RequestParam("teacher_id")String teacher_id,
                             @RequestParam("teacher_pwd")String teacher_pwd
                             ){
        Teacher teacher = new Teacher(teacher_id,teacher_name,teacher_pwd);
        Map<String,String> map = new HashMap<String,String>();
        try{
            loginService.addTeacher(teacher);
            map.put("code","200");
            map.put("msg","添加成功！");
            return map;
        }catch (Exception e){
            if(e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException){   //判断异常类型
                map.put("msg","添加失败,教师id已经存在！");
                return map;
            }else{
                map.put("msg","添加失败,未知类型错误！");
                return map;
            }

        }
    }
    @RequestMapping("/up_teacher_log")
    @ResponseBody
    public Object upTeacher(@RequestParam("new_name")String teacher_name,
                            @RequestParam("teacher_id")String teacher_id,
                            @RequestParam("new_pwd")String teacher_pwd){

        Map<String,String> map = new HashMap<String,String>();
        try{
            loginService.updateTeacher(teacher_name,teacher_pwd,teacher_id);
            map.put("code","200");
            map.put("msg","修改成功！");
            return map;
        }catch (Exception e){
            if(e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException){   //判断异常类型
                map.put("msg","修改失败,教师id不存在！");
                return map;
            }else{
                map.put("msg","修改失败,未知类型错误！");
                return map;
            }

        }

    }
    @RequestMapping("/de_teacher_log")
    @ResponseBody
    public Object deTeacher(@RequestParam("teacher_id")String teacher_id){
        Map<String,String> map = new HashMap<String,String>();

        try{
            loginService.deleteTeacher(teacher_id);
            map.put("code","200");
            map.put("msg","删除成功！");
            return map;
        }catch (Exception e){
            if(e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException){   //判断异常类型
                map.put("msg","删除失败,教师id不存在！");
                return map;
            }else{
                map.put("msg","删除失败,未知类型错误！");
                return map;
            }

        }
    }
    @RequestMapping("/keyTeachers")                                     //获取前端传回的值，并且解码为字符串
    @ResponseBody
    public Object keywordTeachers(@RequestBody String keyword) throws UnsupportedEncodingException {
        this.keyword = URLDecoder.decode(keyword, "UTF-8");
        return "123";
    }

    @RequestMapping("/keyTeacherList")
    @ResponseBody
    public Object keyTeachers(String keyword){                            //前端传回来的值进行模糊查询，返回查询结果
        keyword = this.keyword;
        int l = keyword.length();
        List<Teacher> teacherList = loginService.getTeacher(keyword.substring(0,l-1));
        return teacherList;
    }

    @RequestMapping("/studentList")
    @ResponseBody                                           //向浏览器返回json数据
    public Map<String,Object> studentList(int page , int rows){
        Map<String,Object> map  = new HashMap<>();
        int n = loginService.getSCount();
        List<Student> studentList = loginService.getStudentList(page,rows);
        map.put("rows",studentList);
        map.put("total",n);
        return map;
    }
    @RequestMapping("/deleteStudents")
    @ResponseBody
    public Object deleteStudents(@RequestBody String ids){
        String str = ids.substring(1,ids.length()-1);
        String[] strings = str.split("\\.");
        loginService.deleteSId(strings);
        Map<String,String> map = new HashMap<String,String>();
        map.put("code","200");
        map.put("msg","批量删除成功！");
        return map;
    }
    @RequestMapping("/add_student_log")
    @ResponseBody
    public Object addStudent(@RequestParam("student_name")String student_name,
                             @RequestParam("student_id")Integer student_id,
                             @RequestParam("student_pwd")String student_pwd,
                             @RequestParam("major_id")Integer major_id
    ){
        System.out.println(major_id);
        Student student = new Student(student_id,student_name,student_pwd,major_id);
        Map<String,String> map = new HashMap<String,String>();
        try{
            loginService.addStudent(student);
            map.put("code","200");
            map.put("msg","添加成功！");
            return map;
        }catch (Exception e){
            System.out.println(e);
            if(e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException){   //判断异常类型
                map.put("msg","添加失败,学生已经存在！");
                return map;
            }else{
                map.put("msg","添加失败,未知类型错误！");
                return map;
            }

        }
    }
    @RequestMapping("/up_student_log")
    @ResponseBody
    public Object upStudent(@RequestParam("new_name")String student_name,
                            @RequestParam("student_id")Integer student_id,
                            @RequestParam("new_pwd")String student_pwd,
                            @RequestParam("major_id")Integer major_id){

        Map<String,String> map = new HashMap<String,String>();
        try{
            loginService.updateStudent(student_id,student_name,student_pwd,major_id);
            map.put("code","200");
            map.put("msg","修改成功！");
            return map;
        }catch (Exception e){
            if(e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException){   //判断异常类型
                map.put("msg","修改失败,学生不存在！");
                return map;
            }else{
                map.put("msg","修改失败,未知类型错误！");
                return map;
            }

        }

    }
    @RequestMapping("/de_student_log")
    @ResponseBody
    public Object deStudent(@RequestParam("student_id")Integer student_id){
        Map<String,String> map = new HashMap<String,String>();

        try{
            loginService.deleteStudent(student_id);
            map.put("code","200");
            map.put("msg","删除成功！");
            return map;
        }catch (Exception e){
            if(e.getCause() instanceof java.sql.SQLIntegrityConstraintViolationException){   //判断异常类型
                map.put("msg","删除失败,学生id不存在！");
                return map;
            }else{
                map.put("msg","删除失败,未知类型错误！");
                return map;
            }

        }
    }
    @RequestMapping("/keyStudents")                                     //获取前端传回的值，并且解码为字符串
    @ResponseBody
    public Object keywordStudents(@RequestBody String keyword) throws UnsupportedEncodingException {
        this.studentKey = URLDecoder.decode(keyword, "UTF-8");
        return "123";
    }

    @RequestMapping("/keyStudentList")
    @ResponseBody
    public Object keyStudents(String keyword){                            //前端传回来的值进行模糊查询，返回查询结果
        keyword = this.studentKey;
        int l = keyword.length();
        List<Student> students = loginService.getStudent(keyword.substring(0,l-1));
        return students;
    }
}
